package com.headwind.logistics.order.service.impl;

import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.pojo.ExpressGrant;
import com.headwind.logistics.common.pojo.PutSendInfo;
import com.headwind.logistics.common.util.RedisUtils;
import com.headwind.logistics.order.dto.PlanOrderAddressCustomerDto;
import com.headwind.logistics.common.pojo.BaseNetSite;
import com.headwind.logistics.order.entity.OrderFeeType;
import com.headwind.logistics.order.entity.OrderPack;
import com.headwind.logistics.order.mapper.PlanOrderMappder;
import com.headwind.logistics.order.service.inter.PlanOrderServiceInter;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author 盛夏
 * @Date 2020-07-17 上午 10:47
 * @Version 1.0
 * @DESCRIPTION 填写类作用描述
 */
@Service
public class PlanOrderServiceImpl implements PlanOrderServiceInter {
    @Resource
    private PlanOrderMappder planOrederMapper;


    @Override
    public List<OrderPack> queryOrderPack() {
        return planOrederMapper.queryOrderPack();
    }

    @Override
    public List<OrderFeeType> queryOderFeeType() {
        return planOrederMapper.queryOderFeeType();
    }

    @Override
    public List<PlanOrderAddressCustomerDto> queryPlanOrderPage(Integer page, Integer limit,String username,
                                                                String planOdd,String phone,String orderTime,Long empId) {


        return planOrederMapper.queryPlanOrderPage(page,limit,username,planOdd,phone,orderTime,empId);
    }

    @Override
    public int queryPlanOrderCount() {
        return planOrederMapper.queryPlanOrderCount();
    }

    @Override
    public HashMap<String,List<BaseNetSite>> queryBaseNetSiteById(String area, String address) {
        HashMap<String,List<BaseNetSite>> map = new HashMap<>();

        //起始网点
        String[] strs = address.split("-");
        int length = strs.length-1;

        List<BaseNetSite> purposes = planOrederMapper.queryBaseNetSiteById(strs[length]);


        //目的网点
        String[] split = area.split("-");
        length = split.length-1;
        List<BaseNetSite> starts = planOrederMapper.queryBaseNetSiteById(split[length]);

        //快递单号

        map.put("start",starts);
        map.put("purpose",purposes);
        return map;
    }

    @Override
    public String queryExpressGrant(String useObject) {

        //面单记录对象
        List<ExpressGrant> expressGrants = planOrederMapper.queryExpressGrant(useObject);
        String expressNumber = null;

        if (expressGrants.size() > 0){
            out: for (ExpressGrant expressGrant:expressGrants){
                //起始单号
                Long expressGrantStartNum = Long.parseLong(expressGrant.getExpressGrantStartNum());
                //已使用数量
                int yUse = expressGrant.getYUse();
                //如果使用数量小余发放数量
                if (yUse < expressGrant.getGrantCount()) {
                    expressNumber = "NF" + (expressGrantStartNum + yUse);

                    break out;
                }else {
                    expressNumber = "1";    //
                }
            }
            return expressNumber;
        }else {
            return "0";//没有发放返回0
        }
    }

    @Override
    public PlanOrderAddressCustomerDto queryPlanOrderByOdd(String planOrderOdd) {
        return planOrederMapper.queryPlanOrderByOdd(planOrderOdd);
    }

    @Override
    public int logicDeletePlanOrderByOdd(String planOrderOdd, HttpSession session) {
        Long empId = ((Employee)session.getAttribute("EMPLOYEE")).getEmployeeId();
        int count = 0;
        if (empId == 1){
            count = planOrederMapper.deletePlanOrder(planOrderOdd,((Employee)session.getAttribute("EMPLOYEE")).getEmployeeId());
        }else{
            count =  planOrederMapper.updatePlanOrderByOdd(planOrderOdd,((Employee)session.getAttribute("EMPLOYEE")).getEmployeeId());
        }

        return count;
    }


    @Override
    @Transactional
    public int updatePlanOrderMessage(PlanOrderAddressCustomerDto planDto) {
        int count = planOrederMapper.updatePlanOrderMessage(planDto);

        PlanOrderAddressCustomerDto planOrderDto = planOrederMapper.queryPlanOrderByOdd(planDto.getRecordedNumber());

        PutSendInfo putSendInfo = new PutSendInfo();
        putSendInfo.setPutSendId(planOrderDto.getSendId());
        putSendInfo.setPutSendName(planDto.getSendName());
        putSendInfo.setPutSendPhone(planDto.getSendPhone());
        putSendInfo.setPutSendArea(planDto.getSendArea());
        putSendInfo.setPutSendDetailaddress(planDto.getSendAddress());

        System.out.println("ID"+putSendInfo.getPutSendId()+'\t'+"姓名:"+putSendInfo.getPutSendName()+"\t"+"电话:"+
                putSendInfo.getPutSendPhone()+"\t"+"地址："+putSendInfo.getPutSendArea()+
                putSendInfo.getPutSendDetailaddress());
        //第一次修改寄件信息
        planOrederMapper.updatePutSendInfos(putSendInfo);

        //搜获地址
        String putArea = planDto.getPutArea();

        String split[] = putArea.split("/");
        planDto.setPutArea(split[0]);
        planDto.setPutAddress(split[1]);

        putSendInfo.setPutSendId(planOrderDto.getPutId());
        putSendInfo.setPutSendName(planDto.getPutName());
        putSendInfo.setPutSendPhone(planDto.getPutPhone());
        putSendInfo.setPutSendArea(split[0]);
        putSendInfo.setPutSendDetailaddress(split[1]);

        //第二次修改收货信息
        count = planOrederMapper.updatePutSendInfos(putSendInfo);
        return count;
    }
}
